/*
 * Copyright (C) 2004-2006  Autodesk, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of version 2.1 of the GNU Lesser
 * General Public License as published by the Free Software Foundation.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include <stdafx.h>
#include "FdoOwsBoundingBox.h"
#include "FdoWcsXmlGlobals.h"

FdoOwsBoundingBox::FdoOwsBoundingBox(void) :
mDimensions(0),
    mMinX(0),
    mMinY(0),
    mMaxX(0),
    mMaxY(0)
{
    mLowerCorner = FdoDoubleArray::Create(2);
    mUpperCorner = FdoDoubleArray::Create(2);
}

FdoOwsBoundingBox::~FdoOwsBoundingBox(void)
{
    FDO_SAFE_RELEASE(mLowerCorner);
    FDO_SAFE_RELEASE(mUpperCorner);
}

void FdoOwsBoundingBox::Dispose()
{
    delete this;
}

FdoOwsBoundingBox* FdoOwsBoundingBox::Create()
{
    return new FdoOwsBoundingBox();
}

void FdoOwsBoundingBox::InitFromXml(FdoXmlSaxContext* context, FdoXmlAttributeCollection* attrs)
{
    try {
        VALIDATE_ARGUMENT(attrs);
        VALIDATE_ARGUMENT(context);

        FdoXmlAttributeP att = attrs->FindItem(FdoWcsXmlGlobals::OwsBoundingBoxCRS);
        if (att != NULL) {
            SetCRS(att->GetValue());
        }
        
        att = attrs->FindItem(FdoWcsXmlGlobals::OwsBoundingBoxDimensions);
        if (att != NULL) {
            SetDimensions(FdoCommonOSUtil::wtoi(att->GetValue()));
        }
    }
    catch (FdoException* ex) 
    {
        context->AddError(ex);
        ex->Release();
    }
}

FdoXmlSaxHandler* FdoOwsBoundingBox::XmlStartElement(FdoXmlSaxContext* context, FdoString* uri, FdoString* name, FdoString* qname, FdoXmlAttributeCollection* atts)
{
    FdoXmlSaxHandler* pRet = NULL;

    try
    {
        VALIDATE_ARGUMENT(context);
        VALIDATE_ARGUMENT(name);

        pRet = BaseType::XmlStartElement(context, uri, name, qname, atts);
        if (pRet == NULL)
        {
            if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::OwsBoundingBoxLowerCorner) == 0 ||
                FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::OwsBoundingBoxUpperCorner) == 0)
            {
                mXmlContentHandler = FdoXmlCharDataHandler::Create();
                pRet = mXmlContentHandler;
            }
        }
    }
    catch (FdoException* e)
    {
        context->AddError(e);
        e->Release();
    }

    return pRet;
}

FdoBoolean FdoOwsBoundingBox::XmlEndElement(FdoXmlSaxContext* context, FdoString* uri, FdoString* name, FdoString* qname)
{
    try 
    {
        VALIDATE_ARGUMENT(name);
        VALIDATE_ARGUMENT(context);

        if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::OwsBoundingBoxLowerCorner) == 0) {
            FdoStringsP positions = FdoStringCollection::Create(mXmlContentHandler->GetString(), FdoWcsXmlGlobals::OwsPositionTypeDelimiter);
            double numbers[2];
            numbers[0] = FdoCommonOSUtil::wtof(positions->GetString(0));
            numbers[1] = FdoCommonOSUtil::wtof(positions->GetString(1));

            this->mMinX = numbers[0];
            this->mMinY = numbers[1];
            //mLowerCorner = FdoDoubleArray::Append(mLowerCorner, 2, numbers);
        }
        else if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::OwsBoundingBoxUpperCorner) == 0) {
            FdoStringsP positions = FdoStringCollection::Create(mXmlContentHandler->GetString(), FdoWcsXmlGlobals::OwsPositionTypeDelimiter);
            double numbers[2];
            numbers[0] = FdoCommonOSUtil::wtof(positions->GetString(0));
            numbers[1] = FdoCommonOSUtil::wtof(positions->GetString(1));
            this->mMaxX = numbers[0];
            this->mMaxY = numbers[1];
            //mUpperCorner = FdoDoubleArray::Append(mUpperCorner, 2, numbers);
        }

        FDO_SAFE_RELEASE(mXmlContentHandler.p);
    }
    catch (FdoException* ex) 
    {
        context->AddError(ex);
        ex->Release();
    }

    return BaseType::XmlEndElement(context, uri, name, qname);
}

FdoDoubleArray* FdoOwsBoundingBox::GetLowerCorner() const
{
    return mLowerCorner;
}

void FdoOwsBoundingBox::SetLowerCorner(FdoDoubleArray* value)
{
    mLowerCorner = value;
}

FdoDoubleArray* FdoOwsBoundingBox::GetUpperCorner() const
{
    return mUpperCorner;
}

void FdoOwsBoundingBox::SetUpperCorner(FdoDoubleArray* value)
{
    mUpperCorner = value;
}

FdoString* FdoOwsBoundingBox::GetCRS() const
{
    return mCRS;
}

void FdoOwsBoundingBox::SetCRS(FdoString* value)
{
    mCRS = value;
}

FdoSize FdoOwsBoundingBox::GetDimensions() const
{
    return mDimensions;
}

void FdoOwsBoundingBox::SetDimensions(FdoSize value)
{
    mDimensions = value;
}

FdoDouble FdoOwsBoundingBox::GetMinX() const
{
    return this->mMinX;
}

void FdoOwsBoundingBox::SetMinX(FdoDouble value)
{
    this->mMinX = value;
}

FdoDouble FdoOwsBoundingBox::GetMinY() const
{
    return this->mMinY;
}

void FdoOwsBoundingBox::SetMinY(FdoDouble value)
{

    this->mMinY = value;
}

FdoDouble FdoOwsBoundingBox::GetMaxX() const
{
    return this->mMaxX;
}

void FdoOwsBoundingBox::SetMaxX(FdoDouble value)
{
     mMaxX = value;
}

FdoDouble FdoOwsBoundingBox::GetMaxY() const
{
    return this->mMaxY;
}

void FdoOwsBoundingBox::SetMaxY(FdoDouble value)
{
    this->mMaxY = value;
}